const fs = require('fs')
const path = require('path')
const assert = require('assert')
const { exec } = require('child_process')

describe('Keepfast', function () {
  const projects = ['vue-simple', 'vue-large-entrypoint']

  projects.forEach((project) => {
    describe(project, function () {
      const cwd = path.resolve(__dirname, 'fixtures', project)
      const reportPath = path.resolve(cwd, 'report.json')

      if (fs.existsSync(reportPath)) {
        fs.unlinkSync(reportPath)
      }
      it('should upload message by --use-message option', function (done) {
        this.timeout(10000)
        exec(`keepfast upload --use-message "已开始分析项目：${project}"`, { cwd }, (err) => {
          assert.equal(!err, true)
          done()
        })
      })
      it('should generate report.json', function (done) {
        this.timeout(60000)

        exec('npm install', () => {
          exec(`keepfast report ${reportPath}`, { cwd }, () => {
            assert.equal(fs.existsSync(reportPath), true)
            done()
          })
        })
      })
      it('should upload report.json', function (done) {
        this.timeout(60000)
        exec(`keepfast upload ${reportPath}`, { cwd }, (err) => {
          assert.equal(!err, true)
          done()
        })
      })
    })
  })
})
